package com.tqw.item.service.impl;

import com.tqw.entity.JWTUtil;
import com.tqw.entity.Result;
import com.tqw.item.mapper.AreaMapper;
import com.tqw.item.mapper.ItemDescMapper;
import com.tqw.item.mapper.ItemMapper;
import com.tqw.item.service.ItemDescService;
import com.tqw.item.service.ItemService;
import com.tqw.pojo.Client;
import com.tqw.pojo.Item;
import com.tqw.pojo.ItemDesc;
import com.tqw.pojo.Area;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

import static com.tqw.entity.StatusCode.*;

/**
 * @description: some desc
 * @author: Administrator
 * @date: 2022/4/11 10:22
 */
@Service
public class ItemDescServiceImpl implements ItemDescService {

    @Autowired
    private ItemDescMapper itemDescMapper;
    @Autowired
    private AreaMapper areaMapper;
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Map<String, Object> findItemDesc(Integer itemId) {
        Map<String, Object> map = new HashMap<>();
        ValueOperations<String, Map<String, Object>> vo = redisTemplate.opsForValue();
        Map<String, Object> itemMap = vo.get("itemId:" + itemId);
        Character a = '1';
        if (itemMap == null) {
            System.out.println("走数据库");
            //商品信息
            Item item = itemMapper.selectById(itemId);
            if (!a.equals(item.getStatus())) {
                return null;
            }
            map.put("item", item);
            //获取图片
            ItemDesc itemDesc = itemDescMapper.selectById(itemId);
            if (itemDesc.getImages() != null) {
                String[] images = itemDesc.getImages().split(",");
                map.put("images", images);
            } else {
                String[] images = {"https://img14.360buyimg.com/n7/jfs/t1/162413/39/26752/121447/61a9d7fcE4b747ad8/676584794c81d4a7.jpg"};
                map.put("images", images);
            }
            //商品详情
            map.put("desc", itemDesc.getDescItem());

            //发货地址
            Integer areathree = areaMapper.findthree(itemDesc.getAddressId());
            Area areatwo = areaMapper.findtwo(areathree);
            Area areaone = areaMapper.findone(areatwo.getId());
            String address = areaone.getName() + "-" + areatwo.getName();
            map.put("address", address);
            vo.set("itemId:" + itemId, map, Duration.ofSeconds(30 + (int) (Math.random() * 30)));
        } else {
            System.out.println("走缓存");
            Item item= (Item) itemMap.get("item");
            //商品信息
            if (!a.equals(item.getStatus())) {
                return null;
            }
            map = itemMap;
        }

        return map;
    }

    @Override
    public void updateItemDesc(Integer itemId,Character status) {
        Map<String, Object> map = new HashMap<>();
        ValueOperations<String, Map<String, Object>> vo = redisTemplate.opsForValue();
        Map<String, Object> itemMap = vo.get("itemId:" + itemId);
        if (itemMap!=null){
            Item item= (Item) itemMap.get("item");
            item.setStatus(status);
            map.put("item", item);
            vo.set("itemId:" + itemId, map, Duration.ofSeconds(30 + (int) (Math.random() * 30)));
        }
    }

    @Override
    public Integer findToken(String token, Integer itemId) {
        Map<String, Object> map = new HashMap<>();
        ValueOperations<String, Map<String, Object>> vo = redisTemplate.opsForValue();
        Map<String, Object> itemMap = vo.get("itemId:" + itemId);
        Character a = '1';
        if (itemMap == null) {
            System.out.println("走数据库");
            //商品信息
            Item item = itemMapper.selectById(itemId);
            if (!a.equals(item.getStatus())) {
                return null;
            }
        } else {
            System.out.println("走缓存");
            Item item= (Item) itemMap.get("item");
            //商品信息
            if (!a.equals(item.getStatus())) {
                return null;
            }
        }
        if (!JWTUtil.isExpired(token)){
            Client client = JWTUtil.getClient(token);
            Integer userId = client.getId();
            Item item = itemMapper.selectById(itemId);
            if ((int) userId==(int) item.getUserId()){
                return REPERROR;
            }
            return OK;
        }
        return TOKEN_EXPIRED;
    }
}
